﻿<%@ WebHandler Language="C#" Class="ZSSendRequestHandler" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
/// <summary>
/// add by longb 2022-06-01 
/// 物料配送提请相关业务
/// </summary>
public class ZSSendRequestHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    SendRequestBusiness m_sendData = new SendRequestBusiness();
    uLMSCommonBusiness commonBusiness = new uLMSCommonBusiness();
    uMESContainerReportIBusiness oIBusiness = uMESBusinessFactory.instance.GetContainerReportObject();
    #region 页面初始化
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "GetBigSmallData": //查询大小件信息
                    GetBigSmallData(context);
                    break;
                case "SendRequset": //发起提请
                    SendRequset(context);
                    break;
                case "GetTaskOrderData": //收料确认任务单信息
                    GetTaskOrderData(context);
                    break;
                case "GetApplyBigSmallData": //查询物料配送提请大小件信息
                    GetApplyBigSmallData(context);
                    break;
                case "ReciveSend"://接收配送任务
                    ReciveSend(context);
                    break;
                case "StartSend"://开始配送
                    StartSend(context);
                    break;
                case "GetSpecInfo"://获取session 工序信息
                    GetSpecInfo(context);
                    break;
                case "GetBigSmallConfirmData"://获取生产开工-收料确认信息
                    GetBigSmallConfirmData(context);
                    break;
                case "ReceiveConfirmBigSmall":
                    ReceiveConfirmBigSmall(context);//生产开工-收料确认
                    break;  
                case "GetAllProduct"://查询所有物料信息
                    GetAllProduct(context);
                    break;
                case "GetAllTools":
                    GetAllTools(context);
                    break;
                //----------------临时物料提请--------------
                case "GetTempData"://查询临时物料提请
                    GetTempData(context);
                    break;
                case "DeleteTempData"://删除临时物料提请
                    DeleteTempData(context);
                    break;   
                case "SubmitTempData":
                    SubmitTempData(context);
                    break; 
                case "ReceivedTempData":
                    ReceivedTempData(context);
                    break;
                //-------------工装工具/辅材---------------
                case "GetZsRequiredTools":
                    GetZsRequiredTools(context);
                    break;
                case "GetToolsData":
                    GetToolsData(context);
                    break;
                case "ReciveToolsSend":
                    ReciveToolsSend(context);
                    break;
                case "StartToolsSend":
                    StartToolsSend(context);
                    break;
                case "SaveERPData"://工装工具ERP准备数据保存
                    SaveERPData(context);
                    break;  
                case "GetAuxMaterialData":
                    GetAuxMaterialData(context);
                    break;
                case "ReceiveToolsTransport":
                    ReceiveToolsTransport(context);
                    break;   
                case"GetZsRequiredToolsAll"://工装工具现场配送--查询
                    GetZsRequiredToolsAll(context);
                    break;
                case "GetToolsInProcessCensus"://工装工具在制统计--查询
                    GetToolsInProcessCensus(context);
                    break;
                case "GetToolsProgressCensus"://工装请制进度统计
                    GetToolsProgressCensus(context);
                    break;
                case "GetToolsGiveCheckRemind"://工装工具定检提醒查询
                    GetToolsGiveCheckRemind(context);
                    break;
                case "GetToolsStoreCensus"://工装工具库存统计查询
                    GetToolsStoreCensus(context);
                    break;      
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    #endregion

    #region 生产开工-方法
    /// <summary>
    /// //查询大小件信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetBigSmallData(HttpContext context) {
        uMESProcessResult result = new uMESProcessResult();
        result.Result = true;
        string workflowstepid = context.Request["id"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        string ContainerID = context.Request["ContainerID"].ToString();//add by longb 2023-01-16
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", ContainerNo);//任务单号
        Condition.Add("ContainerID", ContainerID);//任务单ID add by longb 2023-01-16
        Condition.Add("workflowstepid", workflowstepid);
        Condition.Add("mfgorderid", context.Request["mfgorderid"].ToString());
        result.DBTable = m_sendData.GetBigSmallData(Condition);
        result.ReturnData = m_sendData.GetWorkFlowStepAuxMaterialData(Condition);
        DataTable tools = m_sendData.GetWorkFlowStepToolsData(Condition);
        result.DBDataSet.Tables.Add(tools);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 临时物料/工装工具--发起提请
    /// </summary>
    /// <param name="context"></param>
    protected void SendRequset(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        DataTable Aux = JsonConvert.DeserializeObject<DataTable>(context.Request["Aux"].ToString());
        DataTable Tools = JsonConvert.DeserializeObject<DataTable>(context.Request["Tools"].ToString());
       
        var type = context.Request["ApplyType"].ToString();//1:正常配送;2:临时配送
        List<ExcuteEntity> List = new List<ExcuteEntity>();
        if ((dt.Rows.Count + Aux.Rows.Count + Tools.Rows.Count) > 0)
        {
            if (type == "2" && dt.Columns.Contains("Type")&&dt.Rows[0]["Type"].ToString()=="0")//临时工装工具
            {
                var para=new Dictionary<string,string>();
                para.Add("ZSREQUIREDTOOLSTRANSPORTID", Guid.NewGuid().ToString());
                para.Add("ENGINEPARTPLANID",dt.Rows[0]["EnginepartplanID"].ToString());
                para.Add("WORKFLOWSTEPID",dt.Rows[0]["WORKFLOWSTEPID"].ToString());
                para.Add("CREATEEMPLOYEEID",m_UserInfo.EmployeeID.ToString());
                para.Add("TRANSPORTTYPE","1");
                para.Add("ZsRTTransportChildID",Guid.NewGuid().ToString());
                para.Add("ToolID",dt.Rows[0]["Productid"].ToString());
                para.Add("Qty", dt.Rows[0]["FIRSTASSEMBLEQTY"].ToString());
                para.Add("APPLYTYPE", dt.Rows[0]["ApplyType"].ToString());
                para.Add("PURPOSE", dt.Rows[0]["PURPOSE"].ToString());
                para.Add("ProductBatchNo", dt.Rows[0]["ProductBatchNo"].ToString());
                para.Add("STATUS", "25");
                result.Result = m_sendData.InsertZsRequiredToolsTransport(para);
            }
            else
            {
                for (var i = 0; i < dt.Rows.Count; i++)//临时物料
                {
                    var IssueMaterialTransportID = Guid.NewGuid().ToString();
                    var InsertEntity = new ExcuteEntity("IssueMaterialTransport", ExcuteEntity.ExcuteTypeEnum.insert);//4.40-所需物料配送表(现场)
                    List<FieldEntity> InsertList = new List<FieldEntity>();
                    InsertList.Add(new FieldEntity("IssueMaterialTransportID", IssueMaterialTransportID, FieldEntity.FieldTypeEnum.Str));//唯一ID
                    InsertList.Add(new FieldEntity("EnginepartplanID", dt.Rows[i]["EnginepartplanID"].ToString(), FieldEntity.FieldTypeEnum.Str));//任务单ID
                    InsertList.Add(new FieldEntity("IssueERPMaterialID", dt.Rows[i]["IssueERPMaterialID"].ToString(), FieldEntity.FieldTypeEnum.Str));//集件ID
                    InsertList.Add(new FieldEntity("ApplyQty", dt.Rows[i]["FIRSTASSEMBLEQTY"].ToString(), FieldEntity.FieldTypeEnum.Str));//申请配送提请的数量
                    InsertList.Add(new FieldEntity("ApplyType", dt.Rows[i]["ApplyType"].ToString(), FieldEntity.FieldTypeEnum.Str));//申请类型1：正常，2：临时
                    InsertList.Add(new FieldEntity("Status", dt.Rows[i]["ApplyType"].ToString() == "2" ? "25" : "0", FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
                    InsertList.Add(new FieldEntity("WORKFLOWSTEPID", dt.Rows[i]["WORKFLOWSTEPID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                    InsertList.Add(new FieldEntity("PURPOSE", dt.Rows[i]["PURPOSE"].ToString(), FieldEntity.FieldTypeEnum.Str));
                    InsertList.Add(new FieldEntity("CreateDate", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
                    InsertList.Add(new FieldEntity("CreateEmployeeID", m_UserInfo.EmployeeID, FieldEntity.FieldTypeEnum.Str));
                    InsertList.Add(new FieldEntity("ISUSE", "0", FieldEntity.FieldTypeEnum.Str));
                    InsertList.Add(new FieldEntity("ProductName", dt.Rows[i]["ProductName"].ToString(), FieldEntity.FieldTypeEnum.Str));
                    InsertList.Add(new FieldEntity("ProductBatchNo", dt.Rows[i]["ProductBatchNo"].ToString(), FieldEntity.FieldTypeEnum.Str));
                    InsertList.Add(new FieldEntity("Productid", dt.Rows[i]["Productid"].ToString(), FieldEntity.FieldTypeEnum.Str));
                    InsertEntity.ExcuteFileds = InsertList;
                    List.Add(InsertEntity);
                }
                if (Aux.Rows.Count > 0) { List = getRequiredToolsTransport(List, Aux, "2",context); }
                if (Tools.Rows.Count > 0) { List = getRequiredToolsTransport(List, Tools, "1", context); }
                result.Result = DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(List);
            }
        }
        
        var str = type == "1" ? "发起" : "新增";
        result.Message = result.Result ? str+"成功" : str+"失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    private List<ExcuteEntity> getRequiredToolsTransport(List<ExcuteEntity> List, DataTable dt,string TransportType, HttpContext context)
    {
        DataTable dte = commonBusiness.GetTableInfo("ENGINEPARTPLAN", "CONTAINERNO", context.Request["ContainerNo"].ToString());
        string EnginepartplanID = dte.Rows[0]["EnginepartplanID"].ToString();
        for (var i = 0; i < dt.Rows.Count; i++)
        {
            var ZsRequiredToolsTransportID = Guid.NewGuid().ToString();
            var InsertEntity = new ExcuteEntity("ZsRequiredToolsTransport", ExcuteEntity.ExcuteTypeEnum.insert);//
            List<FieldEntity> InsertList = new List<FieldEntity>();
            InsertList.Add(new FieldEntity("ZsRequiredToolsTransportID", ZsRequiredToolsTransportID, FieldEntity.FieldTypeEnum.Str));//唯一ID
            InsertList.Add(new FieldEntity("EnginepartplanID", EnginepartplanID, FieldEntity.FieldTypeEnum.Str));//任务单ID
            InsertList.Add(new FieldEntity("ApplyType", context.Request["ApplyType"].ToString(), FieldEntity.FieldTypeEnum.Str));//申请类型1：正常，2：临时
            InsertList.Add(new FieldEntity("Status", "0", FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
            InsertList.Add(new FieldEntity("WORKFLOWSTEPID", dt.Rows[i]["WORKFLOWSTEPID"].ToString(), FieldEntity.FieldTypeEnum.Str));
            InsertList.Add(new FieldEntity("CreateDate", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
            InsertList.Add(new FieldEntity("CreateEmployeeID", m_UserInfo.EmployeeID, FieldEntity.FieldTypeEnum.Str));
            InsertList.Add(new FieldEntity("ISUSE", "0", FieldEntity.FieldTypeEnum.Str));
            InsertList.Add(new FieldEntity("TransportType", TransportType, FieldEntity.FieldTypeEnum.Str));
            InsertEntity.ExcuteFileds = InsertList;
            List.Add(InsertEntity);
            var InsertEntity1 = new ExcuteEntity("ZsRTTransportChild", ExcuteEntity.ExcuteTypeEnum.insert);//
            List<FieldEntity> InsertList1 = new List<FieldEntity>();
            InsertList1.Add(new FieldEntity("ZsRequiredToolsTransportID", ZsRequiredToolsTransportID, FieldEntity.FieldTypeEnum.Str));//
            InsertList1.Add(new FieldEntity("ZsRTTransportChildID", Guid.NewGuid().ToString(), FieldEntity.FieldTypeEnum.Str));//唯一ID
            InsertList1.Add(new FieldEntity("ToolID", dt.Rows[i]["ToolSID"].ToString(), FieldEntity.FieldTypeEnum.Str));//唯一ID
            InsertList1.Add(new FieldEntity("Qty", dt.Rows[i]["Qty"].ToString(), FieldEntity.FieldTypeEnum.Str));//唯一ID
            InsertEntity1.ExcuteFileds = InsertList1;
            List.Add(InsertEntity1);
        }
        return List;
    }
    
    
    
    /// <summary>
    /// 获取工序session信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetSpecInfo(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        result.ReturnData = context.Session["OPwgResult"];
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    
    #endregion
    
    #region 物料配送管理方法
    /// <summary>
    /// 查询任务单信息
    /// </summary>
    /// <param name="context"></param>
    public void GetTaskOrderData(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", dt.Rows[0]["ContainerNo"].ToString());
        Condition.Add("Machine", dt.Rows[0]["Machine"].ToString());
        Condition.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        Condition.Add("WorkFlowType", dt.Rows[0]["WorkFlowType"].ToString());
        Condition.Add("ApplyType", dt.Rows[0]["ApplyType"].ToString());
        Condition.Add("Status", dt.Rows[0]["Status"].ToString() == "999" ? "" : dt.Rows[0]["Status"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = m_sendData.GetCollectContainerNo(Condition);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取物料配送大小件信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetApplyBigSmallData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string workflowstepid = context.Request["workflowstepid"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", ContainerNo);//任务单号
        Condition.Add("workflowstepid", workflowstepid);
        DataTable dt = m_sendData.GetApplyBigSmallData(Condition);
        DataView big = dt.DefaultView;
        big.RowFilter = "PARTTYPE='大件'";
        result.DBTable = big.ToTable();
        DataView small = dt.DefaultView;
        small.RowFilter = "PARTTYPE='小件'";
        result.ReturnData = small.ToTable();
        DataView temp = dt.DefaultView;
        temp.RowFilter = "APPLYTYPE='2' AND STATUS<>'待提交'";
        result.DBDataSet.Tables.Add(temp.ToTable());
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 接收配送任务
    /// </summary>
    /// <param name="context"></param>
    protected void ReciveSend(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        List<ExcuteEntity> List = new List<ExcuteEntity>();
        if (dt.Rows.Count > 0)
        {
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var InsertEntity = new ExcuteEntity("IssueMaterialTransport", ExcuteEntity.ExcuteTypeEnum.update);//4.40-所需物料配送表(现场)
                List<FieldEntity> updateList = new List<FieldEntity>();
                List<FieldEntity> whereList = new List<FieldEntity>();
                whereList.Add(new FieldEntity("EnginepartplanID", dt.Rows[i]["EnginepartplanID"].ToString(), FieldEntity.FieldTypeEnum.Str));//
                whereList.Add(new FieldEntity("APPLYTYPE", dt.Rows[i]["APPLYTYPE"].ToString()=="临时"?"2":"1", FieldEntity.FieldTypeEnum.Str));//
                whereList.Add(new FieldEntity("WORKFLOWSTEPID", dt.Rows[i]["WORKFLOWSTEPID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                updateList.Add(new FieldEntity("Status", "5", FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
                updateList.Add(new FieldEntity("ReceiveDate", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
                updateList.Add(new FieldEntity("CreateEmployeeID", m_UserInfo.EmployeeID, FieldEntity.FieldTypeEnum.Str));
                InsertEntity.ExcuteFileds = updateList;
                InsertEntity.WhereFileds = whereList;
                //InsertEntity.strWhere = " AND ISSUEERPMATERIALID IN(SELECT ISSUEERPMATERIALID FROM ISSUEERPMATERIAL WHERE WORKFLOWSTEPID='" + dt.Rows[i]["workflowstepid"].ToString() + "')";
                List.Add(InsertEntity);
            }
        }
        result.Result = DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(List);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 开始配送
    /// </summary>
    /// <param name="context"></param>
    protected void StartSend(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string workflowstepid = context.Request["workflowstepid"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        string APPLYTYPE = context.Request["APPLYTYPE"].ToString();
        List<ExcuteEntity> List = new List<ExcuteEntity>();
        if (dt.Rows.Count > 0)
        {
            //循环组装[物料配送表明细]
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var InsertEntity = new ExcuteEntity("IssueMaterialTransportDetail", ExcuteEntity.ExcuteTypeEnum.insert);//4.40-所需物料配送表明细(现场)
                List<FieldEntity> insertList = new List<FieldEntity>();
                insertList.Add(new FieldEntity("IssueMaterialTransportDetailID", Guid.NewGuid().ToString(), FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
                insertList.Add(new FieldEntity("IssueMaterialTransportID", dt.Rows[i]["ISSUEMATERIALTRANSPORTID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("IssueERPMaterialID", dt.Rows[i]["ISSUEERPMATERIALID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("ConfirmQty", dt.Rows[i]["EDITORQTY"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("CreateDate", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
                insertList.Add(new FieldEntity("CreateEmployeeID", m_UserInfo.EmployeeID, FieldEntity.FieldTypeEnum.Str));
                //insertList.Add(new FieldEntity("ISSUEMATERIALTRANSPORTCHILDID", dt.Rows[i]["PRODUCTID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                InsertEntity.ExcuteFileds = insertList;
                List.Add(InsertEntity);
            }
            var Res=DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(List);
            if(Res){
                var para=new Dictionary<String,String>();
                 para.Add("ContainerNo", ContainerNo);//任务单号
                 para.Add("workflowstepid", workflowstepid);
                 para.Add("APPLYTYPE", APPLYTYPE);
                 para.Add("EnginepartplanID", dt.Rows[0]["EnginepartplanID"].ToString().ToString());
                 result.Result = m_sendData.CheckApplyBigSmallSended(para);//返回True:表示配送中，false：配送完
            }
        }
        if (result.Result) result.Message = "配送成功";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 获取生产开工-收料确认信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetBigSmallConfirmData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        result.Result = true;
        string workflowstepid = context.Request["workflowstepid"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        string ContainerID = context.Request["ContainerID"].ToString();//add by longb 2023-01-16
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", ContainerNo);//任务单号
        Condition.Add("ContainerID", ContainerID);//任务单ID add by longb 2023-01-16
        Condition.Add("workflowstepid", workflowstepid);
        DataTable dt = m_sendData.GetBigSmallConfirmData(Condition);
        DataView big = dt.DefaultView;
        big.RowFilter = "PARTTYPE='大件'";
        result.DBTable = big.ToTable();
        DataView small = dt.DefaultView;
        small.RowFilter = "PARTTYPE='小件'";
        result.ReturnData = small.ToTable();
        DataTable dtt = m_sendData.GetRequiredToolsTransportConfirmData(Condition);
        DataView Aux = dtt.DefaultView;
        Aux.RowFilter = "TRANSPORTTYPE='2'";
        result.DBDataSet.Tables.Add(Aux.ToTable());
        DataView Tools = dtt.DefaultView;
        Tools.RowFilter = "TRANSPORTTYPE='1'";
        result.DBDataSet.Tables.Add(Tools.ToTable());
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 生产开工-收料确认(大小件)
    /// </summary>
    /// <param name="context"></param>
    protected void ReceiveConfirmBigSmall(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        List<ExcuteEntity> List = new List<ExcuteEntity>();
        if (dt.Rows.Count > 0)
        {
            //循环组装[物料配送表明细]
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var InsertEntity = new ExcuteEntity("IssueMaterialReceiveDetail", ExcuteEntity.ExcuteTypeEnum.insert);//4.42	所需物料收料表(现场)
                List<FieldEntity> insertList = new List<FieldEntity>();
                insertList.Add(new FieldEntity("ISSUEMATERIALRECEIVEDETAILID", Guid.NewGuid().ToString(), FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
                insertList.Add(new FieldEntity("IssueMaterialTransportID", dt.Rows[i]["ISSUEMATERIALTRANSPORTID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("IssueERPMaterialID", dt.Rows[i]["ISSUEERPMATERIALID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("ReceiveQty", dt.Rows[i]["NNORECEIVEQTY"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("CreateDate", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
                insertList.Add(new FieldEntity("CreateEmployeeID", m_UserInfo.EmployeeID, FieldEntity.FieldTypeEnum.Str));
                InsertEntity.ExcuteFileds = insertList;
                List.Add(InsertEntity);
            }
        }
        uMESProcessResult result = new uMESProcessResult();
        result.Result = DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(List);
        if (result.Result) { result.Message = "收料成功"; UpdateIssueMaterialTransportStatus(context); }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    protected void UpdateIssueMaterialTransportStatus(HttpContext context)
    {
        string workflowstepid = context.Request["workflowstepid"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        string ContainerID = context.Request["ContainerID"].ToString();
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerID", ContainerID);//任务单号
        Condition.Add("ContainerNo", ContainerNo);//任务单号
        Condition.Add("workflowstepid", workflowstepid);
        DataTable dt = m_sendData.GetBigSmallConfirmData(Condition);
        DataView big = dt.DefaultView;
        big.RowFilter = "EDITORQTY>0";//未收料数量大于0，查询结果，为0行时表示，已全部接收，更新配送表为【收料完成】
        if (big.ToTable().Rows.Count==0)
        {
            List<ExcuteEntity> IssueList = new List<ExcuteEntity>();
            var UpdateEntity = new ExcuteEntity("IssueMaterialTransport", ExcuteEntity.ExcuteTypeEnum.update);//4.40-所需物料配送表(现场)
            List<FieldEntity> updateList = new List<FieldEntity>();
            List<FieldEntity> whereList = new List<FieldEntity>();
            whereList.Add(new FieldEntity("EnginepartplanID", dt.Rows[0]["EnginepartplanID"].ToString().ToString(), FieldEntity.FieldTypeEnum.Str));//
            updateList.Add(new FieldEntity("Status", "20", FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
            UpdateEntity.ExcuteFileds = updateList;
            UpdateEntity.strWhere = " AND ISSUEERPMATERIALID IN(SELECT ISSUEERPMATERIALID FROM ISSUEERPMATERIAL WHERE WORKFLOWSTEPID='" + workflowstepid + "')";
            IssueList.Add(UpdateEntity);
           DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(IssueList);
        }
    }
    
    #endregion

    #region 临时配送提请--方法
    /// <summary>
    /// 根据条件查询--大小零件信息
    /// </summary>
    protected void GetAllProduct(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        var name = context.Request["name"].ToString();
        var para=new Dictionary<string,string>();
        para.Add("Name",name);
        result.DBTable = m_sendData.GetAllProduct(para);
        result.Result = result.DBTable.Rows.Count > 0;
        result.Message = result.Result ? "" : "查询不到物料信息";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 根据条件查询--工装工具信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetAllTools(HttpContext context) {
        uMESProcessResult result = new uMESProcessResult();
        var name = context.Request["name"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("Name", name);
        result.DBTable = m_sendData.GetAllTools(para);
        result.Result = result.DBTable.Rows.Count > 0;
        result.Message = result.Result ? "" : "查询不到工装工具信息";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    //查询临时配送物料清单
    protected void GetTempData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string workflowstepid = context.Request["workflowstepid"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", ContainerNo);//任务单号
        Condition.Add("workflowstepid", workflowstepid);
        //-----临时大小件信息
        DataTable dt = m_sendData.GetApplyBigSmallData(Condition);
        DataView temp = dt.DefaultView;
        temp.RowFilter = "APPLYTYPE='2'";
        //-----临时工装工具
        Condition.Add("TransportType", "1");//1:工装
        DataTable dtt = m_sendData.GetZsRequiredToolsTransport(Condition);
        DataView tools = dtt.DefaultView;
        tools.RowFilter = "APPLYTYPE='临时'";
        DataTable dttt = temp.ToTable();
        foreach (DataRow row in dtt.Rows) {
            DataRow NewRow = dttt.NewRow();
            NewRow["ISSUEMATERIALTRANSPORTID"] = row["ZSREQUIREDTOOLSTRANSPORTID"];
            NewRow["PRODUCTBATCHNO"] = row["PRODUCTBATCHNO"];
            NewRow["PRODUCTNAME"] = row["TOOLNAME"];
            NewRow["APPLYTYPE"] ="3";
            NewRow["APPLYQTY"] = row["QTY"];
            NewRow["PURPOSE"] = row["PURPOSE"];
            NewRow["STATUS"] = row["STATUS"];
            NewRow["FULLNAME"] = row["FULLNAME"];
            NewRow["CREATEDATE"] = row["CREATEDATE"];
            dttt.Rows.Add(NewRow);
        }
        result.DBTable = dttt;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    //删除临时配送物料清单
    protected void DeleteTempData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ids = context.Request["ids"].ToString();
        string tids = context.Request["tids"].ToString();
        result.Result = m_sendData.DeleteTempData(ids, tids);
        result.Message = result.Result ? "删除成功" : "删除失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 提交临时配送物料清单
    /// </summary>
    /// <param name="context"></param>
    protected void SubmitTempData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ids = context.Request["ids"].ToString();
        string tids = context.Request["tids"].ToString();
        result.Result = m_sendData.UpdateTempStatus(ids, tids);
        result.Message = result.Result ? "提交成功" : "提交失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 临时配送物料清单--收料确认
    /// </summary>
    /// <param name="context"></param>
    protected void ReceivedTempData(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        string ids = context.Request["ids"].ToString();
        string tids = context.Request["tids"].ToString();
        result.Result = m_sendData.UpdateTempReceiveStatus(ids, tids);
        result.Message = result.Result ? "收料确认成功" : "收料确认失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询--工装工具任务
    /// </summary>
    /// <param name="context"></param>
    public void GetZsRequiredTools(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", dt.Rows[0]["ContainerNo"].ToString());
        Condition.Add("Machine", dt.Rows[0]["Machine"].ToString());
        Condition.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        Condition.Add("WorkFlowType", dt.Rows[0]["WorkFlowType"].ToString());
        Condition.Add("ApplyType", dt.Rows[0]["ApplyType"].ToString());
        Condition.Add("TransportType", context.Request["TransportType"].ToString()); 
        Condition.Add("Status", dt.Rows[0]["Status"].ToString() == "999" ? "" : dt.Rows[0]["Status"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = m_sendData.GetZsRequiredTools(Condition);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询--工装工具申请信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetToolsData(HttpContext context) {
       var para = new Dictionary<string, string>();
       para.Add("ContainerNo", context.Request["ContainerNo"].ToString());//任务单号
       para.Add("workflowstepid", context.Request["workflowstepid"].ToString());
       para.Add("TransportType", context.Request["TransportType"].ToString()); 
       uMESProcessResult result = new uMESProcessResult();
       result.DBTable = m_sendData.GetZsRequiredToolsTransport(para);
       context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 接收--工装工具申请任务
    /// </summary>
    /// <param name="context"></param>
    protected void ReciveToolsSend(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var para = new Dictionary<string, string>();
        para.Add("RECEIVEEMPID", m_UserInfo.EmployeeID.ToString());
        //para.Add("ENGINEPARTPLANID", dt.Rows[0]["ENGINEPARTPLANID"].ToString());
        //para.Add("workflowstepid", dt.Rows[0]["workflowstepid"].ToString());
        para.Add("TransportType", context.Request["TransportType"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.Result = m_sendData.BatchUpdateZsRequiredToolsTransport(dt, para);
        result.Message = result.Result ? "接收成功" : "接收失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 配送--工装工具
    /// </summary>
    /// <param name="context"></param>
    protected void StartToolsSend(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string APPLYTYPE = context.Request["APPLYTYPE"].ToString();
        List<ExcuteEntity> List = new List<ExcuteEntity>();
        if (dt.Rows.Count > 0)
        {
            //循环组装[物料配送表明细]
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var InsertEntity = new ExcuteEntity("ZsRTTransportChildDetail", ExcuteEntity.ExcuteTypeEnum.insert);//4.40-所需物料配送表明细(现场)
                List<FieldEntity> insertList = new List<FieldEntity>();
                insertList.Add(new FieldEntity("ZsRTTransportChildDetailID", Guid.NewGuid().ToString(), FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
                insertList.Add(new FieldEntity("ZsRequiredToolsTransportID", dt.Rows[i]["ZsRequiredToolsTransportID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("ZsRTTransportChildID", dt.Rows[i]["ZsRTTransportChildID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("ConfirmQty", dt.Rows[i]["EDITORQTY"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("CreateDate", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
                insertList.Add(new FieldEntity("CreateEmployeeID", m_UserInfo.EmployeeID, FieldEntity.FieldTypeEnum.Str));
                InsertEntity.ExcuteFileds = insertList;
                List.Add(InsertEntity);
            }
            var Res = DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(List);
            if (Res)
            {
                var para = new Dictionary<String, String>();
                para.Add("workflowstepid", context.Request["workflowstepid"].ToString());
                para.Add("APPLYTYPE", APPLYTYPE);
                para.Add("EnginepartplanID", context.Request["EnginepartplanID"].ToString());
                para.Add("TransportType", context.Request["TransportType"].ToString());
                result.Result = m_sendData.CheckeToolsSended(para);//返回True:表示配送中，false：配送完
            }
            result.Message = Res ? "配送成功" : "配送失败";
        }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 工装工具ERP准备数据保存
    /// </summary>
    /// <param name="context"></param>
    protected void SaveERPData(HttpContext context) {
        uMESProcessResult result = new uMESProcessResult();
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        result.Result = m_sendData.BatchUpdateZsRequiredToolsTransportPrepareQty(dt);
        result.Message = result.Result ? "保存成功" : "保存失败";   
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 查询--材料信息
    /// </summary>
    /// <param name="context"></param>
    protected void GetAuxMaterialData(HttpContext context) {
        var para = new Dictionary<string, string>();
        para.Add("containerNo", context.Request["ContainerNo"].ToString());//任务单号
        para.Add("workflowstepid", context.Request["workflowstepid"].ToString());
        para.Add("TransportType", context.Request["TransportType"].ToString());
        para.Add("ID", context.Request["ID"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = m_sendData.GetAuxMaterialData(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 生产开工-收料确认(工装工具、辅材)
    /// </summary>
    /// <param name="context"></param>
    protected void ReceiveToolsTransport(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        List<ExcuteEntity> List = new List<ExcuteEntity>();
        if (dt.Rows.Count > 0)
        {
            //循环组装[物料配送表明细]
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var InsertEntity = new ExcuteEntity("ZsRTReceiveDetail", ExcuteEntity.ExcuteTypeEnum.insert);//
                List<FieldEntity> insertList = new List<FieldEntity>();
                insertList.Add(new FieldEntity("ZsRTReceiveDetailID", Guid.NewGuid().ToString(), FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
                insertList.Add(new FieldEntity("ZsRequiredToolsTransportID", dt.Rows[i]["ZsRequiredToolsTransportID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("ZsRTTransportChildID", dt.Rows[i]["ZsRTTransportChildID"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("ReceiveQty", dt.Rows[i]["NNORECEIVEQTY"].ToString(), FieldEntity.FieldTypeEnum.Str));
                insertList.Add(new FieldEntity("CreateDate", DateTime.Now, FieldEntity.FieldTypeEnum.DateTime));
                insertList.Add(new FieldEntity("CreateEmployeeID", m_UserInfo.EmployeeID, FieldEntity.FieldTypeEnum.Str));
                InsertEntity.ExcuteFileds = insertList;
                List.Add(InsertEntity);
            }
        }
        uMESProcessResult result = new uMESProcessResult();
        result.Result = DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(List);
        if (result.Result) { result.Message = "收料成功"; UpdateToolsTransportStatus(context); }
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    protected void UpdateToolsTransportStatus(HttpContext context)
    {
        string workflowstepid = context.Request["workflowstepid"].ToString();
        string ContainerNo = context.Request["ContainerNo"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("ContainerNo", ContainerNo);//任务单号
        para.Add("workflowstepid", workflowstepid);
        DataTable dt = m_sendData.GetRequiredToolsTransportConfirmData(para);
        DataView NNORECEIVEQTY = dt.DefaultView;
        NNORECEIVEQTY.RowFilter = "NNORECEIVEQTY>0 AND TransportType='" + context.Request["TransportType"].ToString() + "'";//未收料数量大于0，查询结果，为0行时表示，已全部接收，更新配送表为【收料完成】
        var IDS=string.Empty;
        if (NNORECEIVEQTY.ToTable().Rows.Count == 0)
        {
            DataView RECEIVEQTY = dt.DefaultView;
            RECEIVEQTY.RowFilter = "NNORECEIVEQTY=0 AND TransportType='" + context.Request["TransportType"].ToString() + "'";
            for (var j = 0; j < RECEIVEQTY.ToTable().Rows.Count;j++ )
            {
                IDS += RECEIVEQTY.ToTable().Rows[j]["ZSREQUIREDTOOLSTRANSPORTID"] + ",";
            }
            if (!string.IsNullOrEmpty(IDS))
            {
                List<ExcuteEntity> IssueList = new List<ExcuteEntity>();
                var UpdateEntity = new ExcuteEntity("ZsRequiredToolsTransport", ExcuteEntity.ExcuteTypeEnum.update);//
                List<FieldEntity> updateList = new List<FieldEntity>();
                List<FieldEntity> whereList = new List<FieldEntity>();
                updateList.Add(new FieldEntity("Status", "20", FieldEntity.FieldTypeEnum.Str));//0:提请，5：已接收 10：配送中15：配送完20:收料完成
                UpdateEntity.ExcuteFileds = updateList;
                UpdateEntity.WhereFileds = whereList;
                UpdateEntity.strWhere = " AND ZSREQUIREDTOOLSTRANSPORTID IN('" + IDS.Replace(",","','") + "')";
                IssueList.Add(UpdateEntity);
                DatabaseAccess.DBUtility.DatabaseHelper.ExecuteDataByEntitys(IssueList);
            }
        }
    }
    #endregion

    public void GetZsRequiredToolsAll(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var Condition = new Dictionary<string, string>();
        Condition.Add("ContainerNo", dt.Rows[0]["ContainerNo"].ToString());
        Condition.Add("MfgOrdername", dt.Rows[0]["MfgOrdername"].ToString());
        Condition.Add("TransportType", context.Request["TransportType"].ToString());
        uMESProcessResult result = new uMESProcessResult();
        result.DBTable = m_sendData.GetZsRequiredToolsALL(Condition);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 工装工具在制统计--查询
    /// </summary>
    /// <param name="context"></param>
    public void GetToolsInProcessCensus(HttpContext context) {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("Name", dt.Rows[0]["Name"].ToString());
        para.Add("DrawingNo", dt.Rows[0]["DrawingNo"].ToString());
        para.Add("startedate", dt.Rows[0]["startedate"].ToString());
        para.Add("enddate", dt.Rows[0]["enddate"].ToString());
        para.Add("PageSize", pageSize);
        para.Add("CurrentPageIndex", pageNumber);
        uMESProcessResult result= m_sendData.GetToolsInProcessCensus(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 工装请制进度统计
    /// </summary>
    /// <param name="context"></param>
    private void GetToolsProgressCensus(HttpContext context) 
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var para = new Dictionary<string, string>();
        para.Add("productname", dt.Rows[0]["productname"].ToString());
        if (!string.IsNullOrWhiteSpace(dt.Rows[0]["WorkflowID"].ToString()))
        {
            para.Add("WorkflowID", dt.Rows[0]["WorkflowID"].ToString());
        }
        if (m_UserInfo!=null) {
            para.Add("FactoryName", m_UserInfo.Factory);
            if (!string.IsNullOrWhiteSpace(m_UserInfo.FactoryID)) {
                para.Add("FactoryID", m_UserInfo.FactoryID);
            }
        }
        para.Add("StartDate", dt.Rows[0]["StartDate"].ToString());
        para.Add("EndDate", dt.Rows[0]["EndDate"].ToString());
        para.Add("PageSize", pageSize);
        para.Add("CurrentPageIndex", pageNumber);
        uMESProcessResult result = oIBusiness.GetConReportDetailInfo(para);
        var Columns = new ArrayList();
        for (var i = 1; i < result.DBTable.Columns.Count - 13; i++)
        {
            var dic = new Dictionary<string, string>();
            dic.Add("field", "A" + i);
            if (i == 1) { dic.Add("title", "工序任务"); } else { dic.Add("title", ""); }
            Columns.Add(dic);
        }
        result.ReturnData = Columns;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 工装工具定检提醒查询
    /// </summary>
    /// <param name="context"></param>
    private void GetToolsGiveCheckRemind(HttpContext context) 
    {
        uMESProcessResult result = new uMESProcessResult();
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
    /// <summary>
    /// 工装工具库存统计查询
    /// </summary>
    /// <param name="context"></param>
    private void GetToolsStoreCensus(HttpContext context)
    {
        uMESProcessResult result = new uMESProcessResult();
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
}